package com.sgsg.reflectandfactorymodule;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.sgsg.reflectandfactorymodule.entity.Department;
import com.sgsg.reflectandfactorymodule.entity.User;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/***
 * 反射+抽象工厂模式
 *反射的方式便于对工厂类型进行修改。
 * 因为传递的是字符串，可以直接对字符串进行操作。
 *
 *
 */

public class MainActivity extends AppCompatActivity {

    private Unbinder bind;
    private  String sql = ".sql.";
    private  String access = ".access.";

    @OnClick(R.id.btn1)
    void btn1(){
        try {
            IDepartment instance = DatabaseHelper.createDepartment(this, access);
            instance.insert(new Department());
            instance.getDepartmentType(1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @OnClick(R.id.btn2)
    void btn2(){
        try {
            IUser instance  = DatabaseHelper.createUser(this,access);
            instance.insert(new User());
            instance.getUser(2);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @OnClick(R.id.btn3)
    void btn3(){
        try {
            IDepartment instance = DatabaseHelper.createDepartment(this, sql);
            instance.insert(new Department());
            instance.getDepartmentType(3);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @OnClick(R.id.btn4)
    void btn4(){
        try {
            IUser instance = DatabaseHelper.createUser(this, sql);
            instance.insert(new User());
            instance.getUser(4);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bind=ButterKnife.bind(this);

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }
}
